package com.example.workbench.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.workbench.common.api.CommonResult;
import com.example.workbench.common.command.AddCardCommand;
import com.example.workbench.common.command.DelByIdCommand;
import com.example.workbench.common.command.UpdateCardCommand;
import com.example.workbench.entity.DbTaskCard;
import com.example.workbench.entity.DbTaskLine;
import com.example.workbench.mapper.DbTaskCardMapper;
import com.example.workbench.service.IDbTaskCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-08-03
 */
@Service
public class DbTaskCardServiceImpl extends ServiceImpl<DbTaskCardMapper, DbTaskCard> implements IDbTaskCardService {

    @Override
    public List<DbTaskCard> queryCard(DelByIdCommand command) {
        return getBaseMapper().selectList(new LambdaQueryWrapper<DbTaskCard>()
                .eq(DbTaskCard::getLineId, command.getId())
                .eq(DbTaskCard::getIsDelete, 0)
                .orderByAsc(DbTaskCard::getSort));
    }

    @Override
    public void addCard(AddCardCommand command) {
        List<DbTaskCard> list = getBaseMapper().selectList(new LambdaQueryWrapper<DbTaskCard>()
                .eq(DbTaskCard::getLineId, command.getId())
                .eq(DbTaskCard::getIsDelete, 0));
        int sort = 0;
        if (list.isEmpty()) {
            sort = 1;
        }else {
            sort = list.size() + 1;
        }
        DbTaskCard card = new DbTaskCard();
        card.setCardName(command.getCardName());
        card.setCardTime(null);
        card.setPriority(0);
        card.setLineId(command.getId());
        card.setStyleType(0);
        card.setSort(sort);
        getBaseMapper().insert(card);
    }

    @Override
    public void updateCard(UpdateCardCommand command) {
        DbTaskCard card = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTaskCard>()
                .eq(DbTaskCard::getId, command.getId())
                .eq(DbTaskCard::getIsDelete, 0));
        card.setPriority(command.getPriority());
        card.setCardName(command.getCardName());
        card.setStyleType(command.getStyleType());
        card.setSort(command.getSort());
        getBaseMapper().updateById(card);
    }

    @Override
    public CommonResult deleteCard(DelByIdCommand command) {
        DbTaskCard card = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTaskCard>()
                .eq(DbTaskCard::getId, command.getId())
                .eq(DbTaskCard::getIsDelete, 0));
        if (null == card) {
            return CommonResult.failed("该卡片不存在");
        }
        getBaseMapper().deleteById(card);
        return CommonResult.success("");
    }

    @Override
    public void updateSameLineSort(List<DbTaskCard> list) {
        int sort = 1;
        for (DbTaskCard dbTaskCard : list) {
            DbTaskCard card = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTaskCard>()
                    .eq(DbTaskCard::getId, dbTaskCard.getId())
                    .eq(DbTaskCard::getIsDelete, 0));
            card.setSort(sort);
            card.setLineId(dbTaskCard.getLineId());
            getBaseMapper().updateById(card);
            sort++;
        }
    }
}
